package com.ihome.android.academy;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.ihome.android.EAActivity;
import com.ihome.android.academy.R;
import com.ihome.android.annotation.EAInjectView;
import com.ihome.android.common.ConfigUtil;
import com.ihome.android.util.log.LogUtil;
import com.iptv.player.IPTVPlayer;

public class VideoFullscreenActivity extends EAActivity {
	private final String		TAG					= VideoFullscreenActivity.class.getSimpleName();
	@EAInjectView(id = R.id.main_surface)
	private SurfaceView			mSurfaceView;

	private Handler				mHandler;
	private String				playUrl				= null;
	/******* For video play *********/
	private static final int	MSG_VIDEO_FULLPLAY	= 0X02;
	private IPTVPlayer			mPlayer;
	private SurfaceHolder		mSurfaceHolder		= null;
	private int					mSurfaceWidth		= -1;
	private int					mSurfaceHeight		= -1;
	/*	private boolean mPlayerReleased = false;
		private boolean mNeedStartPlay = false;*/
	public static int			VideoRetryTime		= 0;
	private boolean				mNeedStartPlay		= false;
	private boolean				mVideoInitialized	= false;
	private boolean				mPlayerReleased		= false;
	private boolean				isLive				= false;

	private void procHandler() {
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case MSG_VIDEO_FULLPLAY:
					String url = (String) msg.obj;
					VideoRetryTime = 0;
					//					initPlayer();
					mPlayer.stop();
					mPlayer.setProgramURL(url);
					mPlayer.setLive(isLive);
					mPlayer.start();
					mNeedStartPlay = false;
					break;
				default:
					break;
				}
			}
		};
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.video_fullscreen);
		procHandler();
		init();
	}

	@Override
	public void onPause() {
		super.onPause();
		stopPlayer();
		/*		if (mPlayerReleased == false) {
					Log.d(TAG, " onPause() > releasePlayer");
					releasePlayer();
				}*/
		Log.d(TAG, " onPause()");
	}
	
	@Override
	public void onStop() {
		super.onStop();
		Log.d(TAG, " onStop()");
	}
	
	private void init() {
		playUrl = this.getIntent().getExtras().getString(ConfigUtil.VIDEO_URL_KEY);
		LogUtil.i(TAG, "init() ------- > playUrl = " + playUrl);
		if (playUrl.trim().equals("")) {
			return;
		}
		if (mSurfaceView == null) {
			LogUtil.d(TAG, " --> mSurfaceView == null");
		}
		else {
			initVideo();
		}
	}

	@Override
	public void onDestroy() {
//		releasePlayer();
		stopPlayer();
		super.onDestroy();
	}

	private void startVideoPlay() {
		//		String playUrl = "rtsp://221.228.34.68:554/vod/23380900020010439627.mpg?userid=itvzhengh01&stbip=61.155.239.28&clienttype=1&mediaid=0000000030010009625363&ifcharge=1&time=20150803143724+08&life=172800&usersessionid=31135&vcdnid=vcdn001&boid=001&srcboid=001&columnid=391804&backupagent=221.228.34.68:554&ctype=1&playtype=0&Drm=0&EpgId=null&programid=23380900020010439627&contname=&fathercont=&bp=0&authid=&tscnt=0&tstm=0&tsflow=0&ifpricereqsnd=1&stbid=00100299007011500003C864C79D44D1&nodelevel=3&terminalflag=1&bitrate=0&usercharge=47D3B44B73B209A9CD3BE6DE83553308";

		Message msg = new Message();
		msg.obj = playUrl;
		msg.what = MSG_VIDEO_FULLPLAY;
		mHandler.sendMessageDelayed(msg, 1000);
	}

	@SuppressWarnings("deprecation")
	private void initVideo() {
		if (mSurfaceView == null)
			return;
		if (mPlayer == null) {
			mPlayer = IPTVPlayer.Instance();
		}
		mSurfaceView.getHolder().setFormat(PixelFormat.TRANSPARENT);
		mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		mSurfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
				if (null != holder.getSurface()) {
					mSurfaceHolder = holder;
					if (mSurfaceWidth != width || mSurfaceHeight != height) {
						mVideoInitialized = false;
						mSurfaceWidth = width;
						mSurfaceHeight = height;
					}
					initPlayer();

					if (mNeedStartPlay) {
						mNeedStartPlay = false;
						VideoRetryTime = 0;
						mPlayer.setLive(isLive);
						mPlayer.resume();
						Log.i("test", "onResume2");
					}
				}
			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				// TODO Auto-generated method stub

			}

			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				mSurfaceHolder = null;
				mSurfaceWidth = -1;
				mSurfaceHeight = -1;
//				releasePlayer();
				stopPlayer();
			}
		});

		startVideoPlay();
	}

	private void initPlayer() {
		if (mSurfaceHolder != null && mSurfaceHolder.getSurface() != null && mSurfaceWidth > 0 && mSurfaceHeight > 0) {
			Log.i("test", "initPlayer");
			if (mVideoInitialized == false) {
				mPlayer.initialize();
				mVideoInitialized = true;
				mPlayerReleased = false;
			}
			mPlayer.setSurface(mSurfaceHolder.getSurface());

			int w = mPlayer.getSystemWidth();
			int h = mPlayer.getSystemHeight();

			Log.i("test", "w = " + w + "\th = " + h);
			mPlayer.setScrHW(w, h);

			int[] location = new int[2];
			mSurfaceView.getLocationOnScreen(location);

			/*			mPlayer.setVideoWindow((int) (location[0] * 1.5 + 0.5),
								(int) (location[1] * 1.5 + 0.5),
								(int) (mSurfaceWidth * 1.5 + 0.5),
								(int) (mSurfaceHeight * 1.5 + 0.5));*/
			Log.d(TAG, "location[0] = " + location[0] + "\tlocation[1] = " + location[1] + "\tmSurfaceWidth  = " + mSurfaceWidth + "\tmSurfaceHeight = " + mSurfaceHeight);
			//gy:replace up with following codes for video window right more
			mPlayer.setVideoWindow((int) 0, (int) 0, (int) (mSurfaceWidth), (int) (mSurfaceHeight));

			mPlayer.setAudioBalance(0);
		}
	}

/*	private void releasePlayer() {
		Log.d(TAG, " to releasePlayer");
		IPTVPlayer.setActivityPaused(false);
		if (mPlayerReleased) {
			Log.d(TAG, " releasePlayer > mPlayerReleased == true");
			return;
		}
		mPlayer.setLive(isLive);
		mPlayer.stop();
		mPlayer.release();
		mPlayer = null;
		mVideoInitialized = false;
		mNeedStartPlay = true;
		mPlayerReleased = true;
		Log.d(TAG, "releasePlayer over");
	}*/

	private void stopPlayer() {
		Log.d(TAG, " to stopPlayer");
		mNeedStartPlay = true;
		mPlayer.setLive(isLive);
		mPlayer.stop();
	}

}
